package npr;


import processing.core.*;

import javax.media.opengl.*;
import com.sun.opengl.util.*;
import java.util.LinkedList;
import java.util.ListIterator;
import javax.media.opengl.*;
import com.sun.opengl.util.*;

/** 
 * This finds creases, or sharp angles between normals of adjacent faces of boxy objects.
 * Creases can be precomputed, as they are not view dependent and thus are very fast.
 * Note that this algorithm works only for models loaded from .obj files,
 * not for Processing drawing commands.
 *
 * @see BoxyRidges
 */
public class BoxyValleys extends ObjectSpaceLines{


    /**
     * Specifies whether hidden lines should be stippled.
     */
    boolean stippleHiddenLines;

    /** 
     * Creates a BoxyValleys object.
     */
    public BoxyValleys(){
    }

    /**
     * Specifies whether hidden lines should be stippled.
     *
     * @param stippleHiddenLines true if hidden lines are to be drawn, false if not.
     * The default is false.
     *
     * @see npr.BoxyRidges#drawHiddenLines
     */
    public void stippleHiddenLines(boolean stippleHiddenLines){
	
	this.stippleHiddenLines= stippleHiddenLines;
    }

    
    /**
     * Requests the valleys from a model and draws them with OpenGL
     * visbility test and possibly OpenGL line stippling.
     * You don't have to call this method. It is called by the renderer.
     */
    public void postProcess(){
	
	renderer= (NPR)parent.g;
	gl= renderer.gl;

	OBJModel model;

	for(int i=start; i<stop; i++){
	    float modelview[]= renderer.getModelview(i);
	    gl.glMatrixMode(GL.GL_MODELVIEW);
	    gl.glPushMatrix();
	    gl.glTranslatef(0, 0, width);
	    gl.glMultMatrixf(modelview, 0);
	    ListIterator<OBJModel> listIterator= renderer.getModels(i).listIterator();
	    while(listIterator.hasNext()){    
		model= listIterator.next();
		int valleys= model.getBoxyRidges(false, true, threshold, red, green, blue, alpha);
		if(valleys > 0){
		    if(drawHiddenLines){
			int hiddenValleys= model.getBoxyRidgesHidden(false, true, threshold, hiddenRed, hiddenGreen, hiddenBlue, hiddenAlpha);
			if(stippleHiddenLines){
			    gl.glEnable(GL.GL_LINE_STIPPLE);
			    gl.glLineStipple(1, (short)0x00FF);
			}
			gl.glDisable(GL.GL_DEPTH_TEST);
			gl.glLineWidth(hiddenWidth);
			gl.glCallList(hiddenValleys);
			gl.glDisable(GL.GL_LINE_STIPPLE);
			gl.glEnable(GL.GL_DEPTH_TEST);		       
		    }
		    gl.glLineWidth(width);
		    gl.glCallList(valleys);
		}
	    }
	    gl.glPopMatrix();
	}
    }
}